//package com.mrzhang.glpt.gateway.common.filter;
//
//import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
//import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
//import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
//import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
//import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
//import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
//import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
//import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
//import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
//import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
//import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
//import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
//import com.mrzhang.glpt.gateway.common.fallback.GlptGatewayBlockFallbackProvider;
//import com.netflix.zuul.ZuulFilter;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.annotation.PostConstruct;
//import java.util.HashSet;
//import java.util.Set;
//
///**
// * @author 张霄峰
// * @email 29839415@qq.com
// * @date 2020/4/8 14:50
// */
//@Slf4j
//@Configuration
//public class GlptGatewaySentinelFilter {
//    @Bean
//    public ZuulFilter sentinelZuulPreFilter() {
//        return new SentinelZuulPreFilter();
//    }
//
//    @Bean
//    public ZuulFilter sentinelZuulPostFilter() {
//        return new SentinelZuulPostFilter();
//    }
//
//    @Bean
//    public ZuulFilter sentinelZuulErrorFilter() {
//        return new SentinelZuulErrorFilter();
//    }
//
//    @PostConstruct
//    public void doInit() {
//        ZuulBlockFallbackManager.registerProvider(new GlptGatewayBlockFallbackProvider());
////        initGatewayRules();
//    }
//
//    /**
//     * 定义验证码请求限流，限流规则：
//     *  60秒内同一个IP，同一个 key最多访问 10次
//     */
//    private void initGatewayRules() {
//        Set<ApiDefinition> definitions = new HashSet<>();
//        Set<ApiPredicateItem> predicateItems = new HashSet<>();
//
//        predicateItems.add(new ApiPathPredicateItem().setPattern("/auth/captcha"));
//        ApiDefinition definition = new ApiDefinition("captcha")
//                .setPredicateItems(predicateItems);
//        definitions.add(definition);
//        GatewayApiDefinitionManager.loadApiDefinitions(definitions);
//
//        Set<GatewayFlowRule> rules = new HashSet<>();
//
//        rules.add(new GatewayFlowRule("captcha")
//                .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
//                .setParamItem(
//                        new GatewayParamFlowItem()
//                                .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
//                                .setFieldName("key")
//                                .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT)
//                                .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
//                )
//                .setCount(10)
//                .setIntervalSec(60)
//        );
//        GatewayRuleManager.loadRules(rules);
//    }
//}
